package src.array;

public class no704 {
    //二分查找，区间左闭右开
    public static void main(String[] args) {
        int[] arr = {-1, 0, 3, 5, 9, 12};
        int res = search(arr, 9);
        System.out.println(res);
    }

    public static int search(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;  //由于左闭右开，开始right为6，数组末尾为5，所以不包含区间末尾元素。所以赋值right时，也不能包含区间末尾
            }
        }
        return -1;

    }
}
